package com.liukai.bishe.ui.viewHolder.listener.impl;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.DatePicker;

import com.liukai.bishe.R;
import com.liukai.bishe.model.doman.user.User;
import com.liukai.bishe.presenter.userPresenter.IUpdateUserInfoPresenter;
import com.liukai.bishe.ui.activity.userInfo.ChangeDescribeActivity;
import com.liukai.bishe.ui.activity.userInfo.ChangeNickNameActivity;
import com.liukai.bishe.ui.activity.userInfo.ChangePasswordActivity;
import com.liukai.bishe.ui.dialog.ChangeSexDialog;
import com.liukai.bishe.ui.viewHolder.listener.UserInfoOnClickListener;
import com.liukai.bishe.utils.Constants;


public class UserInfoOnClickListenerImpl implements UserInfoOnClickListener {

    private Activity mActivity;
    private Context mContext;
    private Intent mIntent;
    private IUpdateUserInfoPresenter mIUpdateUserInfoPresenter;

    public UserInfoOnClickListenerImpl(Context context, Activity activity, IUpdateUserInfoPresenter iUpdateUserInfoPresenter) {
        this.mContext = context;
        this.mActivity = activity;
        this.mIUpdateUserInfoPresenter = iUpdateUserInfoPresenter;
    }

    @Override
    public void onItemClickListener(View itemView, String title, String content) {

//        Intent intent = new Intent(mContext,ChangeInfoActivity.class);
//        intent.putExtra("title",title);
//        intent.putExtra("content",content);
//        mActivity.startActivityForResult(intent,Constants.REQUEST_ChANGE_USERINFO_SUCCESS);

        switch (title){
            case "昵称" :
                mIntent = new Intent(mContext,ChangeNickNameActivity.class);
                mIntent.putExtra("title",title);
                mIntent.putExtra("content",content);
                mActivity.startActivityForResult(mIntent,Constants.REQUEST_ChANGE_NICKNAME_SUCCESS);
                break;

            case "性别" :
                changeSexDialog();
                break;

            case "出生年月" :
                changeBirthday();
                break;

            case "个性签名" :
                mIntent = new Intent(mContext,ChangeDescribeActivity.class);
                mIntent.putExtra("title",title);
                mIntent.putExtra("content",content);
                mActivity.startActivityForResult(mIntent,Constants.REQUEST_ChANGE_DESCRIBE_SUCCESS);
                break;

            case "密码" :
                mIntent = new Intent(mContext,ChangePasswordActivity.class);
                mIntent.putExtra("title",title);
                mIntent.putExtra("content",content);
                mActivity.startActivityForResult(mIntent,Constants.REQUEST_ChANGE_PASSWORD_SUCCESS);
                break;
        }

    }

    /**
     * 修改日期
     */
    private void changeBirthday() {

        new DatePickerDialog(mContext, onDateSetListener, 1990, 01, 01).show();

    }

    /**
     * 修改性别
     */
    private void changeSexDialog() {
        ChangeSexDialog changeSexDialog = new ChangeSexDialog(mContext, R.layout.dialog_change_sex, mIUpdateUserInfoPresenter);
        changeSexDialog.show();
    }

    /**
     * 修改日期选择器对话框监听
     */
    private DatePickerDialog.OnDateSetListener onDateSetListener = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            int mYear = year;
            int mMonth = monthOfYear;
            int mDay = dayOfMonth;
            String days;
            if (mMonth + 1 < 10) {
                if (mDay < 10) {
                    days = new StringBuffer().append(mYear).append("年").append("0").
                            append(mMonth + 1).append("月").append("0").append(mDay).append("日").toString();
                } else {
                    days = new StringBuffer().append(mYear).append("年").append("0").
                            append(mMonth + 1).append("月").append(mDay).append("日").toString();
                }

            } else {
                if (mDay < 10) {
                    days = new StringBuffer().append(mYear).append("年").
                            append(mMonth + 1).append("月").append("0").append(mDay).append("日").toString();
                } else {
                    days = new StringBuffer().append(mYear).append("年").
                            append(mMonth + 1).append("月").append(mDay).append("日").toString();
                }

            }
            User user = Constants.getUserInfo();
            user.setBirthday(days);
            mIUpdateUserInfoPresenter.update(user);
        }
    };


}
